<?php
/**
 * +----------------------------------------------------------------------
 * | 美女保佑,永无bug
 * +----------------------------------------------------------------------
 *                      .::::.
 *                    .::::::::.            
 *                    :::::::::::           
 *                 ..:::::::::::'           
 *             '::::::::::::'
 *                .::::::::::
 *           '::::::::::::::..
 *                ..::::::::::::.
 *              ``::::::::::::::::
 *               ::::``:::::::::'        .:::.
 *              ::::'   ':::::'       .::::::::.
 *            .::::'      ::::     .:::::::'::::.
 *           .:::'       :::::  .:::::::::' ':::::.
 *          .::'        :::::.:::::::::'      ':::::.
 *         .::'         ::::::::::::::'         ``::::.
 *     ...:::           ::::::::::::'              ``::.
 *   ```` ':.          ':::::::::'                  ::::..
 *                      '.:::::'                    ':'````..
 * +----------------------------------------------------------------------
 */
namespace app\admin\controller;

// 引入框架内置类
use think\facade\Request;

// 引入表格和表单构建器
use app\common\facade\MakeBuilder;
use app\common\builder\FormBuilder;
use app\common\builder\TableBuilder;
use think\facade\View;

class AdminCeshi extends Base
{
    // 验证器
    protected $validate = 'AdminCeshi';

    // 当前主表
    protected $tableName = 'admin_ceshi';

    // 当前主模型
    protected $modelName = 'AdminCeshi';

    public function _initialize() {
        parent::_initialize();
    }

    public function zidingyiyemian(){
       View::assign('name','新建控制器指定模板文件-》根目录-》view-》admin-》adminceshi->index.html');
        return View::fetch('index');
    }

    // 列表
    public function index(){
        // 获取主键
        $pk = MakeBuilder::getPrimarykey($this->tableName);
        // 获取列表数据
        $columns = MakeBuilder::getListColumns($this->tableName);
        // 获取搜索数据
        $search = MakeBuilder::getListSearch($this->tableName);
        // 获取当前模块信息
        $model = '\app\common\model\\' . $this->modelName;
        $module = \app\common\model\Module::where('table_name', $this->tableName)->find();
        // 检测单页模式
        $isSingle = MakeBuilder::checkSingle($this->modelName);
        if ($isSingle) {
            return redirect($isSingle);
        }
        // 搜索
         // 搜索
        if (Request::param('getList') == 1) {
            $where         = MakeBuilder::getListWhere($this->tableName);
            $orderByColumn = Request::param('orderByColumn') ?? $pk;
            $isAsc         = Request::param('isAsc') ?? 'desc';
            return $model::getList($where, $this->pageSize, [$orderByColumn => $isAsc]);
        }
        // 构建页面
        return TableBuilder::getInstance()
            ->setUniqueId($pk)                              // 设置主键
            ->setPageTitle('<div class="row"><div class="col-sm-6"><h1 class="m-0">后台新建测试模块<small>副标题</small></h1></div><div class="col-sm-6"><ol class="breadcrumb float-sm-right"><li class="breadcrumb-item"><a href="/admin/index/index.html">Home</a></li></ol></div></div>')
            ->addColumns($columns)                          // 添加列表字段数据
            ->setSearch($search)                            // 添加头部搜索
            ->addColumn('right_button', '操作', 'btn')      // 启用右侧操作列
            ->addRightButton('info', [                      // 添加额外按钮
                'title' => '添加',
                'icon'  => 'fa fa-plus',
                'class' => 'btn btn-success btn-xs',
                'href'  => url('add', ['parentId' => '__id__'])
            ])
            ->addRightButtons($module->right_button)        // 设置右侧操作列
            ->addTopButtons($module->top_button)            // 设置顶部按钮组
            ->addTopButton('default', [
                'title'   => '展开/折叠',
                'icon'    => 'fas fa-exchange-alt',
                'class'   => 'btn btn-info treeStatus',
                'href'    => '',
                'onclick' => '$.operate.treeStatus()'
            ]) // 自定义按钮
            /*->addTopButton('default', [
                'title'   => '批量新增',
                'icon'    => 'fa fa-plus',
                'class'   => 'btn btn-success',
                'href'    => '',
                'onclick' => '$.operate.batchAdd(\'' . url('batchAdd') . '\')'
            ])*/ // 自定义按钮
            ->setPagination('false')                        // 关闭分页显示
            ->fetch();
    }

    // 添加
    public function add()
    {
        $color = ['1' => '绿色', '2' => '红色', '3' => '黄色'];
        $sex = ['0' => '保密', '1' => '男的', '2' => '女的'];
        $selectt = ['1' => '下拉1111', '2' => '下拉2222', '3' => '下拉3333'];
        // 获取字段信息
        $columns = MakeBuilder::getAddColumns($this->tableName);
        foreach ($columns as $key => $value) {
            if($key == 1){
                $columns[1][4]=$sex;
            }

            if($key == 2){
                $columns[2][4]=$color;
            }
            if($key == 4){
                $columns[4][4]=$selectt;
            }

        }
        //p($columns);die;
        // 提示信息
        $pageTips = '添加提示';
   
        // 获取分组后的字段信息
        $groups = MakeBuilder::getAddGroups($this->modelName, $this->tableName, $columns);
        
        // 构建页面
        $builder = FormBuilder::getInstance();
        $builder->setPageTips($pageTips, 'warning');  // 提示信息
        $builder->addHidden('hiddeninput', 'hiddeninput');
        //$builder->addSelect('my_selects', '下拉菜单', '提示信息', ['1' => '绿色', '2' => '红色', '3' => '黄色'], '', '', '', '', true);
        // $builder->addRadio('sex', '性别', 'sex提示信息', $color, '0', 'extra_attr', 'extra_class', true);
        // $builder->addCheckbox('duoxuan', '多选', '', $color);
        // $builder->addSelect('my_selects', '下拉菜单', '提示信息', ['1' => '绿色', '2' => '红色', '3' => '黄色']);
        $groups ? $builder->addGroup($groups) : $builder->addFormItems($columns);
        return $builder->fetch();
    }

    // 添加保存
    public function addPost()
    {
        if (Request::isPost()) {
            $data = MakeBuilder::changeFormData(Request::except(['file'], 'post'), $this->tableName);

            $result = $this->validate($data, $this->validate);
            if (true !== $result) {
                // 验证失败 输出错误信息
                $this->error($result);
            } else {
                $model = '\app\common\model\\' . $this->modelName;
                $result = $model::addPost($data);
                if ($result['error']) {
                    $this->error($result['msg']);
                } else {
                   
                    $this->success($result['msg'], 'index');
                }
            }
        }
    }

     // 修改
    public function edit(string $id)
    {
        $model = '\app\common\model\\' . $this->modelName;
        $info = $model::edit($id)->toArray();
        // 获取字段信息
        $columns = MakeBuilder::getAddColumns($this->tableName, $info);

        $color = ['1' => '绿色', '2' => '红色', '3' => '黄色'];
        $sex = ['0' => '保密', '1' => '男的', '2' => '女的'];
        $selectt = ['1' => '下拉1111', '2' => '下拉2222', '3' => '下拉3333'];
       
        foreach ($columns as $key => $value) {
            if($key == 2){
                $columns[2][4]=$sex;
            }

            if($key == 3){
                $columns[3][4]=$color;
            }
            if($key == 5){
                $columns[5][4]=$selectt;
            }

        }

    //p($columns);die;
        // 获取分组后的字段信息
        $groups = MakeBuilder::getAddGroups($this->modelName, $this->tableName, $columns);

        // 构建页面
        $builder = FormBuilder::getInstance();
        $groups ? $builder->addGroup($groups) : $builder->addFormItems($columns);
        return $builder->fetch();
    }

    // 修改保存
    public function editPost()
    {
        if (Request::isPost()) {
            $data = MakeBuilder::changeFormData(Request::except(['file'], 'post'), $this->tableName);
           
            $result = $this->validate($data, $this->validate);
            if (true !== $result) {
                // 验证失败 输出错误信息
                $this->error($result);
            } else {
                $model = '\app\common\model\\' . $this->modelName;
                $result = $model::editPost($data);
                if ($result['error']) {
                    $this->error($result['msg']);
                } else {
                    $this->success($result['msg'], 'index');
                }
            }
        }
    }

    

}
